<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"/>
    <meta content="telephone=no,email=no" name="format-detection">
    <!--禁止自动识别电话号码和邮箱-->
    <meta content="yes" name="apple-mobile-web-app-capable">
    <!--苹果手机：会删除默认的工具栏和菜单栏，网站开启对web app程序的支持-->
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <!--苹果手机：在web app应用下状态条（屏幕顶部条）的颜色,默认值为default（白色），可以定为black（黑色）和black-translucent（灰色半透明）。-->
    <meta name="apple-touch-fullscreen" content="yes"/>
    <!--苹果手机：如果把一个web app添加到了主屏幕中，那么从主屏幕中打开这个web app则全屏显示-->
    <link rel="stylesheet" type="text/css" href="css/shopinformation.css">
    <title>店铺信息</title>
    <script src="js/jquery-3.3.1.js"></script>
    <script src="js/layer.js"></script>
    <script src="js/utils.js"></script>
    <script src="js/service.js"></script>
    <script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
</head>
<body>
<div id="vueData">
    <header>
        <div class="title_header dis_flex">
            <div class="dis_flex" @click="goBack">
                <img src="img/return_2.png">
            </div>
            <div>
                <span>店铺信息</span>
            </div>
            <div>
                <span class="col_cheng" @click="saveStoreInfo">修改</span>
            </div>
        </div>
    </header>
    <div class="body">
        <div class="div_li">
            <img :src="storeImg?storeImg:'img/rectangle_3.png'"
                 @click="onLoadImg">
            <div class="dis_flex">
                <input v-model="storeInfo.sellerName"/>
                <input type="text" placeholder="备注:" v-model="storeInfo.notice">
            </div>
        </div>
        <div class="div_li dis_flex">
            <span>店家姓名：</span>
            <span class="col_hui">{{storeInfo.name}}</span>
        </div>
        <div class="div_li dis_flex">
            <span>余额：</span>
            <span class="col_hui">{{storeInfo.money}}</span>
        </div>
        <div class="div_li dis_flex">
            <span>营业开始时间：</span>
            <input type="time" v-model="storeInfo.startDate">
        </div>
        <div class="div_li dis_flex">
            <span>营业结束时间：</span>
            <input type="time" v-model="storeInfo.endDate">
        </div>
        <div class="div_li dis_flex">
            <span>电话：</span>
            <span class="col_hui">{{storeInfo.phone}}</span>
        </div>
        <div class="div_li dis_flex">
            <span>店铺类型：</span>
            <select v-if="storeInfo.sellerType==10" v-model="storeTypeIndex" name="cars" @change="selStoreType">
                <option value="-1">请选择</option>
                <option v-for="(item,index) in storeType" :value="index">{{item.category_name}}</option>
            </select>
            <select v-else style="background-color: #EEEEEE;" disabled="disabled" v-model="storeTypeIndex" name="cars">
                <option v-for="(item,index) in storeType" :value="index">{{item.category_name}}</option>
            </select>
        </div>
        <div class="div_li dis_flex">
            <span>几元起送：</span>
            <select v-model="upSendIndex" name="cars" @change="selUpSend">
                <option v-for="(item,index) in upSend" :value="index">{{item.category_name}}元起送</option>
            </select>
        </div>
        <div class="div_li dis_flex">
            <span>地址:</span>
            <span class="col_hui" v-if="storeInfo.address" @click="chooseAddress">{{storeInfo.address}}</span>
            <span class="col_hui" @click="chooseAddress" v-else>请选择地址</span>
        </div>
    </div>
    <footer>
        <div class="first_div" v-if="storeInfo.businessSign=='0'" @click="switchBusinessSign('1')">营业中</div>
        <div @click="switchBusinessSign('0')" v-else>休息中</div>
    </footer>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    let localStore = JSON.parse(localStorage.localStore);
    let href = location.href.split('#')[0];
    let addrName = GetQueryString('name');
    let addr = GetQueryString('addr');
    let latng = GetQueryString('latng');
    if (addrName) {
        addrName = decodeURIComponent(addrName);
        addr = decodeURIComponent(addr);
    }
    let vm = new Vue({
        el: '#vueData',
        data: {
            storeInfo: {category_name: 'ddd'},
            storeType: [],
            storeTypeIndex: -1,
            avatarUrl: null,
            storeImg: null,
            upSend: [],
            upSendIndex: 0
        }, watch: {
            storeInfo: {
                handler(val, oldVal) {
                    localStorage.localStoreInfo = JSON.stringify(val);
                },
                deep: true
            }, storeType: {
                handler(val, oldVal) {
                    localStorage.localStoreType = JSON.stringify(val);
                },
                deep: true
            }, 'storeTypeIndex': function (res) {
                localStorage.localStoreTypeIndex = res;
            }, 'avatarUrl': function (res) {
                wx.uploadImage({
                    localId: res, // 需要上传的图片的本地ID，由chooseImage接口获得
                    success: function (res) {
                        vm.storeInfo.sellerPicture = res.serverId; // 返回图片的服务器端ID
                    }
                })
            }, 'upSendIndex': function (res) {
                localStorage.localStoreUpSendIndex = res;
            }, upSend: {
                handler(val, oldVal) {
                    localStorage.localUpSend = JSON.stringify(val);
                },
                deep: true
            }
        }, methods: {
            selUpSend(e) {
                let index = e.target.value;
                vm.upSendIndex = index;
                vm.storeInfo.distribution = vm.upSend[index].id;
            },
            goBack() {
                localStorage.removeItem('localStoreInfo');
                localStorage.removeItem('localStoreTypeIndex');
                localStorage.removeItem('localStoreType');
                localStorage.removeItem('storeImg');
                localStorage.removeItem('localStoreUpSendIndex');
                localStorage.removeItem('localUpSend');
                location.href = 'Administration.html';
            }, chooseAddress() {
                location.href = 'https://3gimg.qq.com/lightmap/components/locationPicker2/index.html?type=0&backurl=https://yapinkeji.com/shanduonewretail/shopinformation.html&key=Q74BZ-4AT34-QCUU5-XADLJ-XTGQZ-65FQM&referer=near-store';
            }, selStoreType(e) {
                let index = e.target.value;
                if (index > -1) {
                    vm.storeTypeIndex = index;
                    vm.storeInfo.sellerType = vm.storeType[index].id;
                }
            }, onLoadImg() {
                wx.chooseImage({
                    count: 1,
                    sizeType: ['compressed'],
                    success: function (res) {
                        vm.avatarUrl = res.localIds[0];
                        let u = navigator.userAgent;//手机类型android或ios
                        if (u.indexOf('iPhone') > -1) {
                            wx.getLocalImgData({
                                localId: res.localIds[0], // 图片的localID
                                success: function (res) {
                                    vm.storeImg = res.localData;

                                }
                            });
                        } else {
                            vm.storeImg = res.localIds[0];
                        }
                        localStorage.storeImg = vm.storeImg;
                    }
                });
            }, saveStoreInfo() {
                $.ajax({
                    url: '/shanduonewretail/jseller/updatesellerdetails',
                    data: {
                        token: localStore.token,
                        userSeller: JSON.stringify(vm.storeInfo)
                    }, success: function (res) {
                        if (res.success) {
                            localStorage.removeItem('localStoreInfo');
                            localStorage.removeItem('localStoreTypeIndex');
                            localStorage.removeItem('localStoreType');
                            localStorage.removeItem('storeImg');
                            localStorage.removeItem('localStoreUpSendIndex');
                            localStorage.removeItem('localUpSend');
                            toast('修改成功，正在跳转...');
                            setTimeout(function () {
                                location.href = 'Administration.html';
                            }, 1000);
                        } else {
                            toast('修改未完成');
                        }
                    }
                });
            }, switchBusinessSign(businessSign) {
                layer.open({
                    content: vm.storeInfo.businessSign === '0' ? "打烊关店?" : "开店营业?"
                    , btn: ['是的', '取消']
                    , yes: function (index) {
                        $.get('/shanduonewretail/jseller/updatebusinesssign', {
                            token: localStore.token, businessSign: vm.storeInfo.businessSign === '0' ? '1' : '0'
                        }, function (res) {
                            if (res.success) {
                                vm.storeInfo.businessSign = businessSign;
                            }
                        });
                        layer.close(index);
                    }
                });

            }
        }
    });
    if (addrName) {
        initData();
        if ('我的位置' === addrName) {
            vm.storeInfo.address = addr;
        } else {
            vm.storeInfo.address = addrName;
        }
        vm.storeInfo.lon = latng.split(',')[1];
        vm.storeInfo.lat = latng.split(',')[0];
    }

    function initData() {
        if (localStorage.storeImg) {
            vm.storeImg = localStorage.storeImg;
        }
        if (localStorage.localStoreInfo) {
            vm.storeInfo = JSON.parse(localStorage.localStoreInfo);
            vm.storeType = JSON.parse(localStorage.localStoreType);
            vm.storeTypeIndex = localStorage.localStoreTypeIndex;
            vm.upSend = JSON.parse(localStorage.localUpSend);
            vm.upSendIndex = localStorage.localStoreUpSendIndex;
        } else {
            loadStoreDetail({
                token: localStore.token,
                typeId: 1
            }, function (res) {
                if (res.success) {
                    vm.storeInfo = res.result;
                    vm.storeInfo.endDate = getMyDate(vm.storeInfo.endDate);
                    vm.storeInfo.startDate = getMyDate(vm.storeInfo.startDate);
                    if (vm.storeInfo.sellerPicture) {
                        vm.storeImg = '/yapingzh/picture/' + vm.storeInfo.sellerPicture;
                        localStorage.storeImg = vm.storeImg;
                    }
                    $.getJSON('/shanduonewretail/jseller/selectselleralltype', function (res) {
                        if (res.success) {
                            vm.storeType = res.result;
                            $.each(vm.storeType, function (i, item) {
                                if (item.id === vm.storeInfo.sellerType) {
                                    vm.storeTypeIndex = i;
                                    return false;
                                }
                            });

                        }
                    });
                    $.getJSON('/shanduonewretail/jseller/selectdistributiontype', function (res) {
                        if (res.success) {
                            vm.upSend = res.result;
                            $.each(vm.upSend, function (i, item) {
                                if (vm.storeInfo.distribution == item.category_name) {
                                    vm.upSendIndex = i;
                                    return false;
                                }
                            })
                        }
                    });
                } else {
                    toast('登录状态失效')
                }
            }, function () {
                toast('请求错误');
            });
        }
    }

    $(function () {
        initData();
        init_wx_js_sdk(href, function (result) {
            //初始化接口环境
            wx.config({
                appId: result.appid,
                timestamp: result.timestamp,
                nonceStr: result.noncestr,
                signature: result.signature,
                jsApiList: ['chooseImage', 'uploadImage', 'hideMenuItems']
            });
            wx.ready(function () {
                wx.hideMenuItems({
                    menuList: ['menuItem:copyUrl', 'menuItem:share:qq', 'menuItem:share:QZone', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari', 'menuItem:share:email', 'menuItem:readMode']
                });
            });
        });

        pushHistory();
        window.addEventListener("popstate", function (e) {
            localStorage.removeItem('localStoreInfo');
            localStorage.removeItem('localStoreTypeIndex');
            localStorage.removeItem('localStoreType');
            localStorage.removeItem('storeImg');
            localStorage.removeItem('localStoreUpSendIndex');
            localStorage.removeItem('localUpSend');
            location.href = 'Administration.html';
        }, false);
    });

    function pushHistory() {
        window.history.pushState({
            title: "title",
            url: "#"
        }, "title", "#");
    }
</script>
</html>